package com.lv.controller;

import com.alibaba.fastjson.JSONObject;
import com.lv.data.Result;
import com.lv.model.TbSpecificationOption;
import com.lv.service.SpecService;
import com.lv.vo.SellerVo;
import com.lv.vo.SpecVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {
    @Resource
    private SpecService ss;
    //查询
    @GetMapping
    public Result getList(){
        List<SpecVo> vo = ss.getList();
        return Result.OK().setData(vo);
    }

    //增加
    @PostMapping
    public Result addSpec(@RequestParam String specName,@RequestParam String arrStr){
        SpecVo vo = new SpecVo();
        vo.setSpecName(specName);
        List<TbSpecificationOption> optionList = JSONObject.parseArray(arrStr, TbSpecificationOption.class);
        ss.addSpecAndOption(vo,optionList);
        return Result.OK();
    }

    //回显
    @PostMapping("toupdate")
    public Result toupdate(@RequestParam Long id){
        SpecVo vo = ss.toupdate(id);
        return Result.OK().setData(vo);
    }

    //修改
    @PutMapping
    public Result updateSpec(@RequestBody SpecVo vo){
        ss.updateSpec(vo);
        return Result.OK();
    }

    //删除
    @DeleteMapping
    public Result deleteSpec(@RequestParam Long[] ids){
        ss.deleteSpec(ids);
        return Result.OK();
    }


}
